<template>
  <div class="container">
    <div class="hd">
      <TsButton
        perms="price:post"
        label="添加"
        type="primary"
        size="small"
        @click="add"
      ></TsButton>
      <TsButton
        perms="price:delete"
        label="删除"
        type="danger"
        size="small"
        @click="delMute"
      ></TsButton>
    </div>
    <!--    表格区域-->
    <div
      class="table"
      style="height: calc(100% - 160px); margin-top: 20px; overflow-y: auto"
    >
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        :header-cell-style="{ background: '#FEF6F6' }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column
          prop="price"
          label="价格(元)"
          width="200"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="defaultComboName"
          label="默认套餐"
          width="200"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column prop="canteenName" label="食堂" width="200">
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="240px">
          <template #default="scope">
            <TsButton
              perms="price:$id:put"
              style="color: #e6a23c"
              type="text"
              size="mini"
              label="修改"
              @click="update(scope.row.priceId)"
            ></TsButton>
            <TsButton
              perms="price:delete"
              style="color: #f56c6c"
              type="text"
              size="mini"
              label="删除"
              @click="del(scope.row.priceId)"
            ></TsButton>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--    分页区域-->
    <el-pagination
      style="float: right; margin: 20px"
      background
      v-model:currentPage="pageNum"
      v-model:page-size="pageSize"
      layout=" prev, pager, next"
      :total="totalNum"
      @size-change="pageSizeChange"
      @current-change="pageCurrentChange"
    />
    <!--    新增编辑对话框-->
    <el-dialog
      v-model="dialogVisible"
      :title="priceId ? '编辑餐标' : '新增餐标'"
      width="30%"
      :before-close="handleClose"
    >
      <el-form :model="mealForm" label-width="80px" ref="mealRef">
        <el-form-item
          label="套餐标准"
          prop="price"
          :rules="[
            { required: true, message: '请输入价格' },
            { type: 'number', message: '必须是一个数字' },
          ]"
        >
          <el-input
            v-model.number="mealForm.price"
            size="small"
            style="width: 214px"
            placeholder="请输入套餐标准"
          ></el-input>
        </el-form-item>
        <el-form-item label="食堂选择" prop="priceCanteenId">
          <el-select
            style="width: 214px"
            v-model="mealForm.priceCanteenId"
            class="m-2"
            placeholder="请选择食堂"
            size="small"
          >
            <el-option
              v-for="item in canteenOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="默认套餐" prop="defaultCombo">
          <el-select
            style="width: 214px"
            v-model="mealForm.defaultCombo"
            class="m-2"
            placeholder="请选择套餐  "
            size="small"
          >
            <el-option
              v-for="item in defalutOptions"
              :key="item.comboId"
              :label="item.comboName"
              :value="item.comboId"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button size="small" type="primary" @click="submit"
            >提交</el-button
          >
          <el-button size="small" @click="handleClose">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import commonPageRepository from "@/composables/common-page-repository";
import {
  mealList,
  canteenList,
  addPrice,
  delPrice,
  priceInfo,
  updatePrice,
  defalutMeal,
} from "@/api/canteen/mealLabel";
//分页查询数据
import { onMounted, ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
const pageNum = ref(1);
const pageSize = ref(50);
const totalNum = ref(0);
//表格数据
const tableData = ref([]);
//获取餐标分页数据
function getMealList() {
  mealList({
    pageNum: pageNum.value,
    pageSize: pageSize.value,
  }).then((res) => {
    tableData.value = res.data.content;
    totalNum.value = res.data.totalElements;
  });
}
//改变分页事件
const { pageSizeChange, pageCurrentChange } = commonPageRepository(
  pageSize,
  pageNum,
  getMealList
);
//对话框显示与隐藏
const dialogVisible = ref(false);
//餐标数据
const mealForm = ref({});
const mealRef = ref(null);
//对话框关闭之前的回调
function handleClose() {
  mealRef.value.resetFields();
  dialogVisible.value = false;
  priceId.value = "";
}
//新增方法
function add() {
  dialogVisible.value = true;
}
//食堂数据
const canteenOptions = ref([]);
//获取食堂数据
function getCanteenList() {
  canteenList().then((res) => {
    canteenOptions.value = res.data;
  });
}
//提交表单数据
function submit() {
  if (!priceId.value) {
    addPrice({
      price: mealForm.value.price,
      priceCanteenId: mealForm.value.priceCanteenId,
    }).then((res) => {
      if (res.code === 200) {
        ElMessage.success("添加成功");
        mealRef.value.resetFields();
        dialogVisible.value = false;
        priceId.value = "";
        getMealList();
      }
    });
  } else {
    updatePrice(priceId.value, {
      defaultCombo: mealForm.value.defaultCombo,
      price: mealForm.value.price,
      priceCanteenId: mealForm.value.priceCanteenId,
    }).then((res) => {
      if (res.code === 200) {
        ElMessage.success("更新成功");
        mealRef.value.resetFields();
        dialogVisible.value = false;
        priceId.value = "";
        getMealList();
      }
    });
  }
}
//多选数组
const muteArr = ref([]);
//多选操作
function handleSelectionChange(val) {
  muteArr.value = val.map((item) => item.priceId).join(",");
}
//多选删除
function delMute() {
  if (muteArr.value.length === 0) {
    ElMessage.warning("请选择数据后再试");
    return;
  }
  delPrice({
    ids: muteArr.value,
  }).then((res) => {
    if (res.code === 200) {
      ElMessage.success("删除成功");
      getMealList();
    }
  });
}
//价格ID
const priceId = ref("");
//默认套餐数据
const defalutOptions = ref([]);
//修改餐标信息
function update(id) {
  priceId.value = id;
  dialogVisible.value = true;
  priceInfo(id).then((res) => {
    mealForm.value = res.data;
    mealForm.value.priceCanteenId = res.data.priceCanteenId * 1;
  });
  defalutMeal(priceId.value).then((res) => {
    defalutOptions.value = res.data;
  });
}
//单个删除
function del(id) {
  ElMessageBox.confirm("您确定要删除这条数据么?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delPrice({
        ids: id,
      }).then((res) => {
        if (res.code === 200) {
          ElMessage.success("删除成功");
          getMealList();
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消操作",
      });
    });
}
onMounted(() => {
  getMealList();
  getCanteenList();
});
</script>

<style scoped lang="scss">
.container {
  height: calc(100vh - 200px);
  background: #ffffff;
  box-shadow: 0 0 16px rgba(96, 96, 96, 0.16);
  border-radius: 6px;
  padding: 20px;
  overflow-y: auto;
  margin-top: 20px;
  .hd {
    display: flex;
    align-items: center;
    justify-content: end;
  }
}
</style>
